package com.ehualu.getes.until;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.valuecount.ParsedValueCount;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.*;

public class EsUtiluser {


    //    private static String host = "37.158.97.130,37.158.97.131,37.158.97.132";
    private static String host = "172.16.101.53,172.16.101.54,172.16.101.55";
    private static int port = 9200;
    private static String schema = "http";


    public void getshij(String dlname, Integer kknum, RestHighLevelClient client) {
        try {

//            String indexName = index;

            SearchRequest searchRequest = new SearchRequest("guoche_201911");
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//			sourceBuilder.from(0); // 默认0
            sourceBuilder.size(10000);// 默认10条

            QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.termQuery("qxdm.keyword", "371525"))
//                    .must(QueryBuilders.rangeQuery("createtime").from(starttime).to(endTimeStr));
                    .must(QueryBuilders.matchPhraseQuery("ccollectionaddress", dlname));
            sourceBuilder.query(queryBuilder);
            searchRequest.source(sourceBuilder);
            SearchResponse response = client.search(searchRequest);
            Long rtlh = response.getHits().getTotalHits();
            System.out.println("查询数据量：" + rtlh);

//            Integer daynum = 122;
//
//            Long avgd = (rtlh / daynum) / kknum;
//
//            System.out.println(dlname + "路均值流量：" + avgd);

//			return pic;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public void teram(RestHighLevelClient client) {
        try {
//            SearchRequest searchRequest = new SearchRequest("guoche_201911");
            SearchRequest searchRequest = new SearchRequest("guoche_201912");

            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

            //			sourceBuilder.from(0); // 默认0
//            searchSourceBuilder.size(100);// 默认10条
            //根据姓名进行分组统计个数
            TermsAggregationBuilder field = AggregationBuilders.terms("terms_caddresscode").field("caddresscode.keyword");
            ValueCountAggregationBuilder countField = AggregationBuilders.count("count_caddresscode").field("caddresscode.keyword");

            field.subAggregation(countField);
            field.size(Integer.MAX_VALUE);
            sourceBuilder.aggregation(field);
            searchRequest.source(sourceBuilder);
            SearchResponse response = client.search(searchRequest);
            //分组在es中是分桶

            ParsedStringTerms termsName = response.getAggregations().get("terms_caddresscode");
            List<? extends Terms.Bucket> buckets = termsName.getBuckets();


            for (int i = 0; i < buckets.size(); i++) {


                String forkey = (String) buckets.get(i).getKey();

                if (forkey.length() != 13) {

                    System.out.println(forkey);
                    ParsedValueCount countName = buckets.get(i).getAggregations().get("count_caddresscode");
                    double value = countName.value();
//                    System.out.println(value);
                }

            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 关闭连接
     */
//    public void close(RestHighLevelClient client) {
//        if (client != null) {
//            try {
//                client.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
    public static void main(String[] args) {

        ESClientbui esClientbui = new ESClientbui();
        RestHighLevelClient client = esClientbui.getHighLevelClient(host, port, schema);


        EsUtiluser esUtiluser = new EsUtiluser();
//        esUtiluser.getshij("北环", 84,client);

//        esUtiluser.teram(client);
        esUtiluser.getshij("化村卡口北向南",1,client);
        esClientbui.close(client);


    }
}
